﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using MySqlQueryToolkit;
using Rhino.Mocks;

namespace MySqlQueryToolkitTests
{
    [TestClass]
    public class QueryHandlerTests
    {
        [TestMethod]
        public void CreateInstanceExpectNoException()
        {
            Assert.IsNotNull(new QueryHandler(null, null, null));
        }

        [TestMethod]
        public void GetViewDataWithAllFalseSettingsExpectNoCalls()
        {
            var explainMock = MockRepository.GenerateMock<IExplainQueryReporter>();
            var target = new QueryHandler(explainMock, null, null);

            target.GetViewData("SELECT * FROM tblTest", false, false, false);

            explainMock.AssertWasNotCalled(e => e.Report(Arg<string>.Is.Anything));
        }

        [TestMethod]
        public void GetViewDataWithExplainExpectCall()
        {
            const string query = "SELECT * FROM tblTest";
            var explainMock = MockRepository.GenerateMock<IExplainQueryReporter>();
            var target = new QueryHandler(explainMock, null, null);

            target.GetViewData(query, true, false, false);

            explainMock.AssertWasCalled(e => e.Report(query));
        }

        [TestMethod]
        public void GetViewDataWithProfileExpectCall()
        {
            const string query = "SELECT * FROM tblTest";
            var profileMock = MockRepository.GenerateMock<IProfileQueryReporter>();
            var target = new QueryHandler(null, profileMock, null);

            target.GetViewData(query, false, true, false);

            profileMock.AssertWasCalled(e => e.Report(query));
        }

        [TestMethod]
        public void GetViewDataWithTimeExpectCall()
        {
            const string query = "SELECT * FROM tblTest";
            var timeMock = MockRepository.GenerateMock<ITimeQueryReporter>();
            var target = new QueryHandler(null, null, timeMock);

            target.GetViewData(query, false, false, true);

            timeMock.AssertWasCalled(e => e.Report(query));
        }

        [TestMethod]
        public void GetViewDataWithExplainAndProfileExpectCalls()
        {
            const string query = "SELECT * FROM tblTest";
            var explainMock = MockRepository.GenerateMock<IExplainQueryReporter>();
            var profileMock = MockRepository.GenerateMock<IProfileQueryReporter>();
            var target = new QueryHandler(explainMock, profileMock, null);

            target.GetViewData(query, true, true, false);

            explainMock.AssertWasCalled(e => e.Report(query));
            profileMock.AssertWasCalled(e => e.Report(query));
        }

        [TestMethod]
        public void GetViewDataWithProfileAndTimeExpectCalls()
        {
            const string query = "SELECT * FROM tblTest";
            var profileMock = MockRepository.GenerateMock<IProfileQueryReporter>();
            var timeMock = MockRepository.GenerateMock<ITimeQueryReporter>();
            var target = new QueryHandler(null, profileMock, timeMock);

            target.GetViewData(query, false, true, true);

            timeMock.AssertWasCalled(e => e.Report(query));
            profileMock.AssertWasCalled(e => e.Report(query));
        }

        [TestMethod]
        public void GetViewDataWithExplainAndProfileAndTimeExpectCalls()
        {
            const string query = "SELECT * FROM tblTest";
            var explainMock = MockRepository.GenerateMock<IExplainQueryReporter>();
            var profileMock = MockRepository.GenerateMock<IProfileQueryReporter>();
            var timeMock = MockRepository.GenerateMock<ITimeQueryReporter>();
            var target = new QueryHandler(explainMock, profileMock, timeMock);

            target.GetViewData(query, true, true, true);

            explainMock.AssertWasCalled(e => e.Report(query));
            profileMock.AssertWasCalled(e => e.Report(query));
            timeMock.AssertWasCalled(e => e.Report(query));
        }
    }
}
